﻿
package extremefx.browser {
	import flash.external.ExternalInterface;	
	import flash.display.Stage;	
	import flash.events.MouseEvent;	
	import flash.display.InteractiveObject;	
	
	/**
	 * @author Marcelo Volmaro
	 */
	public final class MacMouseFix {
		static private var _init:Boolean = false;
		static private var _currItem:InteractiveObject;
		static private var _clonedEvent:MouseEvent;
		
		public static function init(stage:Stage):void {
			if (_init) return;
			
			_init = true;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, function(e:MouseEvent):void {
				_currItem = InteractiveObject(e.target);
				_clonedEvent = MouseEvent(e);
			});
				
			if (ExternalInterface.available) {
				var id:String = 'efx_' + Math.floor(Math.random()*1000000);
				ExternalInterface.addCallback(id, function():void{});
				ExternalInterface.call(_macMouseFix);
				ExternalInterface.call("efx.initMacMouseWheel", id);
				ExternalInterface.addCallback('externalMouseEvent', _externalMouseEvent);	
			}
		}
		
		static private function _externalMouseEvent(delta:Number):void {
			if(_currItem && _clonedEvent)
				_currItem.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_WHEEL, true, false, 
					_clonedEvent.localX, _clonedEvent.localY, _clonedEvent.relatedObject,
					_clonedEvent.ctrlKey, _clonedEvent.altKey, _clonedEvent.shiftKey, _clonedEvent.buttonDown,
					int(delta)));
		}
			
		private static var _macMouseFix:XML = <script><![CDATA[function() {
	if(!efx) efx = {};

	// find the function we added
	efx.findSwf = function(id) {
		var objects = document.getElementsByTagName("object");
		for(var i = 0; i < objects.length; i++)
			if(typeof objects[i][id] != "undefined")
				return objects[i];
		
		var embeds = document.getElementsByTagName("embed");
		for(var j = 0; j < embeds.length; j++)
			if(typeof embeds[j][id] != "undefined")
				return embeds[j];
			
		return null;
	};
		
	efx.initMacMouseWheel = function(id) {	
		var userAgent = navigator.userAgent.toLowerCase(),
		swf = efx.findSwf(id);
		
		if(swf && /mac/.test(userAgent)) {
			
			var mouseOver = false;

			/// Mouse move detection for mouse wheel support
			function _mousemove(event) {
				mouseOver = event && event.target && (event.target == swf);
			}

			/// Mousewheel support
			var _mousewheel = function(event) {
				if(mouseOver) {
					var delta = 0;
					if(event.wheelDelta)		delta = event.wheelDelta / (/opera/.test(userAgent) ? 12 : 120);
					else if(event.detail)		delta = -event.detail;
					if(event.preventDefault)	event.preventDefault();
					swf.externalMouseEvent(delta);
					return true;
				}
				return false;
			}

			// install mouse listeners
			if(typeof window.addEventListener != 'undefined') {
				window.addEventListener('DOMMouseScroll', _mousewheel, false);
				window.addEventListener('DOMMouseMove', _mousemove, false);
			}
			
			window.onmousewheel = document.onmousewheel = _mousewheel;
			window.onmousemove = document.onmousemove = _mousemove;
		}
	}
}
]]></script>;
	}
}
